package me.khrystal.rxnews.module.news.ui.adapter;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.socks.library.KLog;

import java.util.Collections;
import java.util.List;

import me.khrystal.rxnews.R;
import me.khrystal.rxnews.base.BaseRecyclerAdapter;
import me.khrystal.rxnews.base.BaseRecyclerViewHolder;
import me.khrystal.rxnews.callback.SimpleItemTouchHelperCallback;
import me.khrystal.rxnews.greendao.NewsChannelTable;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/18
 * update time:
 * email: 723526676@qq.com
 */
public class NewsChannelAdapter extends BaseRecyclerAdapter<NewsChannelTable>
        implements SimpleItemTouchHelperCallback.OnMoveAndSwipeListener{


    private SimpleItemTouchHelperCallback mItemTouchHelperCallback;

    private OnItemMoveListener mItemMoveListener;

    public NewsChannelAdapter(Context context, List<NewsChannelTable> data,boolean useAnimation) {
        super(context, data,useAnimation);
    }

    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_news_channel;
    }

    @Override
    public NewsChannelHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER){
            return new NewsChannelHolder(mContext,
                    mInflater.inflate(R.layout.item_footer,parent,false));
        }else {
            final NewsChannelHolder holder = new NewsChannelHolder(mContext,
                    mInflater.inflate(getItemLayoutId(viewType),parent,false));
            if (mClickListener != null){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mClickListener.onItemClick(v,holder.getLayoutPosition());
                    }
                });
            }
            if (mItemTouchHelperCallback != null){
                holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        //触摸事件发生时 如果是锁死的频道 不允许拖拽
                        if (mData.get(holder.getLayoutPosition()).getNewsChannelFixed()){
                            KLog.e("锁死的频道 不允许拖拽");
                            mItemTouchHelperCallback.setLongPressDragEnabled(false);
                            return true;
                        }else {
                            mItemTouchHelperCallback.setLongPressDragEnabled(true);
                        }
                        return false;
                    }
                });
            }
            return holder;
        }
    }

    @Override
    public void bindData(BaseRecyclerViewHolder holder, int position, NewsChannelTable item) {
        holder.getTextView(R.id.tv_news_channel).setText(item.getNewsChannelName());
        holder.getTextView(R.id.tv_news_channel).setSelected(item.getNewsChannelFixed());
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        //前三个固定不能交换
        if (!mData.get(fromPosition).getNewsChannelFixed() && !mData.get(toPosition).getNewsChannelFixed()){
            Collections.swap(mData,fromPosition,toPosition);
            notifyItemMoved(fromPosition,toPosition);

            if (mItemMoveListener != null){
                mItemMoveListener.onItemMove(fromPosition,toPosition);
            }
            return true;
        }
        return false;
    }

    @Override
    public void onItemDismiss(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public void setItemTouchHelper(SimpleItemTouchHelperCallback callback1){
        mItemTouchHelperCallback = callback1;
    }


    private class NewsChannelHolder extends BaseRecyclerViewHolder implements SimpleItemTouchHelperCallback.OnStateChangedListener{

        public NewsChannelHolder(Context context, View itemView) {
            super(context, itemView);
        }

        @Override
        public void onItemSelected() {
//            enable false change background
            itemView.setEnabled(false);
        }

        @Override
        public void onItemClear() {
            itemView.setEnabled(true);
        }
    }

    public void setItemMoveListener(OnItemMoveListener itemMoveListener){
        mItemMoveListener = itemMoveListener;
    }

    public interface OnItemMoveListener{
        void onItemMove(int fromPosition ,int toPosition);
    }
}
